<template>
  <div>
    <!-- 卡片区域 -->
    <el-card class="box-card">
      <div slot="header" class="clearfix box-header">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addVisible = true">添加分类</el-button>
      </div>
      <!-- 表格区域 -->
      <el-table :data="cateList" border stripe style="width: 100%">
        <el-table-column type="index" label="序号"> </el-table-column>
        <el-table-column prop="cate_name" label="分类名称" width="180"> </el-table-column>
        <el-table-column prop="cate_alias" label="分类别名"> </el-table-column>
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button type="primary" size="mini" @click="showUpdate(row)">修改</el-button>
            <el-button type="danger" size="mini" @click="remove(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加的对话框 -->
    <el-dialog
      title="添加分类"
      :visible.sync="addVisible"
      width="35%"
      :close-on-click-modal="false"
      @closed="onAddClosed"
    >
      <!-- 添加分类的form表单 -->
      <el-form :model="addForm" :rules="formRules" label-width="80px" ref="addFormRef">
        <el-form-item prop="cate_name" label="分类名称">
          <el-input v-model="addForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="addForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false" size="mini">取 消</el-button>
        <el-button type="primary" size="mini" @click="addNewCate">添加</el-button>
      </span>
    </el-dialog>

    <!-- 修改的对话框 -->
    <el-dialog title="修改" :visible.sync="editVisible" width="35%" :close-on-click-modal="false">
      <el-form :model="editForm" label-width="80px" :rules="formRules" ref="editFormRef">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="editForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="editForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false" size="mini">取 消</el-button>
        <el-button type="primary" @click="updateCate" size="mini">修 改</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getCateListAPI, addCateAPI, updateCateAPI, deleteCateAPI } from '@/api/article.js'
export default {
  name: 'ArtCate',
  created() {
    this.initCateList()
  },
  data() {
    return {
      // 文章分类的列表数据
      // 思路:
      // 1、封装获取文章分类数据的API接口
      // 2、在当前组件中，按需导入接口
      // 3、在methods中封装一个函数，调用API接口，请求分类的列表数据
      // 4、在created生命周期函数中，调用步骤三封装的methods
      // 5、
      cateList: [],
      // 控制添加对话框的显示与隐藏
      addVisible: false,
      // 控制修改对话框的显示与隐藏
      editVisible: false,
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      // 修改表单的数据对象
      editForm: {
        id: '',
        cate_name: '',
        cate_alias: ''
      },
      formRules: {
        cate_name: [
          { required: true, message: '分类名称是必填项', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '分类名称必须是1-10位的非空字符', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '分类别名是必填项', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类别名必须是1-15位的字母和数字', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async initCateList() {
      const { data: res } = await getCateListAPI()
      if (res.code !== 0) return this.$message.error(res.message)
      this.cateList = res.data
    },
    onAddClosed() {
      this.$refs.addFormRef.resetFields()
    },
    // 添加新分类
    addNewCate() {
      // 对表单数据进行校验
      // 校验失败直接return
      // 校验成功调用API接口发起添加分类的请求
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await addCateAPI(this.addForm)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.addVisible = false
        this.initCateList()
      })
    },
    // 点击按钮，展示修改的对话框
    showUpdate(row) {
      // 1、先获取到要修改的分类的id
      // 2、判断id是否为1和2
      // 如果是则展示提示消息
      if (row.id === 1 || row.id === 2) return this.$message.error('修改分类失败')
      this.editForm = Object.assign({}, row)
      this.editVisible = true
    },
    updateCate() {
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await updateCateAPI(this.editForm)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.editVisible = false
        this.initCateList()
      })
    },
    async remove(id) {
      if (id === 1 || id === 2) return this.$message.error('管理员数据不允许删除')
      const result = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (result === 'cancel') return
      const { data: res } = await deleteCateAPI(id)
      if (res.code !== 0) return this.$message.error('删除失败')
      this.$message.success('删除成功')
      this.initCateList()
    }
  }
}
</script>

<style lang="less">
.box-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
